package rcs34.android.AndroidRC;

import rcs34.android.AndroidRC.IPRegister.IPPermision;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Activity_IpAuth extends ListActivity{
  
  private Activity_IpAuth aThis;
  private IPAdapter ipAdapter;
  
  private class Enabler implements View.OnClickListener{
    private String ip; 
    public Enabler(String _ip){
      ip=_ip;
    }
    
    @Override
    public void onClick(View _v) {
      CheckBox check=(CheckBox)_v;
      GApp.ipRegister.put(ip, check.isChecked());
      ipAdapter.notifyDataSetChanged();
      aThis.getListView().invalidateViews();
    }
    
  }
  
  private class IPAdapter extends BaseAdapter{

    @Override
    public int getCount() {
      return GApp.ipRegister.size();
    }

    @Override
    public Object getItem(int _position) {
      return GApp.ipRegister.get(_position);
    }

    @Override
    public long getItemId(int _position) {
      return _position;
    }

    @Override
    public int getItemViewType(int arg0) {
      return 0;
    }

    @Override
    public View getView(int _position, View arg1, ViewGroup arg2) {
     IPPermision ip=GApp.ipRegister.get(_position);
      LayoutInflater inflater = (LayoutInflater)aThis.getSystemService
          (Context.LAYOUT_INFLATER_SERVICE);
      RelativeLayout vIp=(RelativeLayout) inflater.inflate(aThis.getResources().getLayout(R.layout.ipauth_item), null);
      TextView ipView=(TextView) vIp.findViewById(R.id.ipauthitemip);
      CheckBox bEnable=(CheckBox) vIp.findViewById(R.id.ipauthitemcheck);
      bEnable.setChecked(ip.enabled);
      bEnable.setOnClickListener(new Enabler(ip.ip));
      ipView.setText(ip.ip);
      return vIp;
    }

    @Override
    public int getViewTypeCount() {
      return 1;
    }

    @Override
    public boolean hasStableIds() {
      return false;
    }

    @Override
    public boolean isEmpty() {
      return GApp.ipRegister.size()<1;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver arg0) {
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver arg0) {
    }

    @Override
    public boolean areAllItemsEnabled() {
      return true;
    }

    @Override
    public boolean isEnabled(int position) {
      return true;
    }
    
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    aThis=this;
  }

  public void onResume() {
    super.onResume();
    setContentView(R.layout.activity_ipauth);
    ipAdapter=new IPAdapter();
    this.getListView().setAdapter(ipAdapter);
  }

  public void onPause() {
    super.onPause();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activitymain, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_main_settings:
      Intent i = new Intent(this, Activity_Preferences.class);
      startActivity(i);
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

}
